package
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.NetStatusEvent;
	import flash.media.Camera;
	import flash.media.Microphone;
	import flash.media.Video;
	import flash.net.NetConnection;
	import flash.net.NetStream;

	public class MinRecord extends Sprite
	{
		private var nc:NetConnection;

		private var ns:NetStream;

		private var rtmpNow:String;

		private var msg:Boolean;

		private var cam:Camera;

		private var mic:Microphone;

		private var vid1:Video;

		//Constructor
		public function MinRecord()
		{
			//NetConnection.defaultObjectEncoding = flash.net.ObjectEncoding.AMF0;
			nc = new NetConnection();
			nc.addEventListener(NetStatusEvent.NET_STATUS, checkConnect);
			rtmpNow = "rtmp://192.168.1.209/vodTest";
			//rtmpNow="rtmp:/vid2″;
			nc.connect(rtmpNow);
			addMedia();
			this.stage.addEventListener(MouseEvent.MOUSE_DOWN, startRecord);
			this.stage.addEventListener(MouseEvent.MOUSE_UP, stopRecord);
		}

		private function addMedia():void
		{
			cam = Camera.getCamera();
			cam.setMode(240, 180, 24);
			cam.setQuality(0, 90);
			mic = Microphone.getMicrophone();
			vid1 = new Video(cam.width, cam.height);
			vid1.attachCamera(cam);
			addChild(vid1);
			vid1.x = 100;
			vid1.y = 50;
		}

		private function checkConnect(e:NetStatusEvent):void
		{
			msg = (e.info.code == "NetConnection.Connect.Success");
			if (msg)
			{
				ns = new NetStream(nc);
			}
		}

		private function startRecord(e:Event):void
		{
			if (ns)
			{
				ns.attachAudio(mic);
				ns.attachCamera(cam);
				ns.publish("test", "record");
			}
		}

		private function stopRecord(e:Event):void
		{
			ns.close();
		}
	}
}
